package com.taobao.wolfwolf.web.servler;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;

import com.taobao.wolfwolf.hibernate.service.ShopperService;
import com.taobao.wolfwolf.util.FetchData;

public class Shopper extends HttpServlet {

	private static final long serialVersionUID = 1112744013154103102L;
	private static Logger logger = Logger.getLogger(Shopper.class);

	public Shopper() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ApplicationContext context = org.springframework.web.context.support.WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		ShopperService shopperService = (ShopperService) context.getBean("ShopperService");
		
		com.taobao.wolfwolf.hibernate.pojo.Shopper newer_shopper = new FetchData().getShopper();
		com.taobao.wolfwolf.hibernate.pojo.Shopper laster_shopper = shopperService.getLastShopper();
		
		if(!newer_shopper.equals(laster_shopper)) {
			shopperService.create(newer_shopper);
		} else  {
			laster_shopper.setCredit(newer_shopper.getCredit());
			laster_shopper.setFavour(newer_shopper.getFavour());
			laster_shopper.setQuantity(newer_shopper.getQuantity());
			shopperService.saveOrUpdate(laster_shopper);
		}
		
		List<com.taobao.wolfwolf.hibernate.pojo.Shopper> shoppers = shopperService.findAll();
		Collections.reverse(shoppers);
		
		String nextLevelText = getNextLevelText(laster_shopper);
		int nextLevelVal = getNextLeveltVal(laster_shopper);
		
		request.getSession().setAttribute("shoppers", shoppers);
		request.getSession().setAttribute("nextLevelText", nextLevelText);
		request.getSession().setAttribute("nextLevelVal", nextLevelVal);
		
		response.sendRedirect("../shopper.jsp");
	}
	
	/**
	 * 获取下次等级的文本标识
	 * @return
	 * 		等级的文本标识
	 */
	private String getNextLevelText(com.taobao.wolfwolf.hibernate.pojo.Shopper laster_shopper) {
		return calcNextLevel(laster_shopper.getCredit())[0];
	}
	
	private int getNextLeveltVal(com.taobao.wolfwolf.hibernate.pojo.Shopper laster_shopper) {
		return Integer.valueOf(calcNextLevel(laster_shopper.getCredit())[1]);
	}
	
	/**
	 * 计算还差多少信誉升级
	 * @param credit
	 * @return
	 */
	private String[] calcNextLevel(int credit) {
		
		if(credit < 5000 && credit > 2000) {
			return new String[]{"5钻", "5000"};
		} else if(credit > 5000 && credit < 10000) {
			return new String[]{"1皇冠", "10000"};
		} else if(credit > 10000 && credit < 20000) {
			return new String[]{"2皇冠", "20000"};
		} else if(credit > 20000 && credit < 50000) {
			return new String[]{"3皇冠", "50000"};
		} else if(credit > 500000 && credit < 100000) {
			return new String[]{"4皇冠", "100000"};
		}  
		
		return null;
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
